package freejim.icu.highschoolhealthstatuspost.service;

import freejim.icu.highschoolhealthstatuspost.dao.*;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class StatusService {

    @Resource
    InfoRepository infoRepository;

    @Resource
    DetailInfoRepository detailInfoRepository;

    @Resource
    DailyPostRepository dailyPostRepository;


    @Resource
    UserRepository userRepository;

    public boolean recordPost(HttpServletRequest request){
        int loginType = Integer.parseInt(request.getParameter("loginType"));
        long userid = 2;
        if (loginType == 1){
            userid = getUserId(request);

        }
        else if (loginType == 3){
            userid = getAuthUserId(request);
        }
        System.out.println("userid is "+userid);
        if(!addDailyPost(request, userid)){
            return false;
        }
        updateDetailInfo(request, userid);
        updateInfo(request, userid);
        return true;
        
    }

    private long getAuthUserId(HttpServletRequest request) {
        String userid = request.getParameter("userid");
        return userRepository.getIdWithUserId(userid);
    }

    private long getUserId(HttpServletRequest request) {
        String username = request.getParameter("username");
        User user = userRepository.findByName(username);
        return user.getId();
    }

    private void updateDetailInfo(HttpServletRequest request, long infoId) {
        DetailInfo detailInfo = detailInfoBuilder(request, infoId);
        detailInfoRepository.save(detailInfo);
    }

    private boolean addDailyPost(HttpServletRequest request,long userid) {
        DailyPost dailyPost = dailyPostBuilder(request, userid);
        if (dailyPost == null)return false;
        dailyPostRepository.save(dailyPost);
        return true;
    }


    private void updateInfo(HttpServletRequest request,long id) {
        Info info = infoBuilder(request, id);
        infoRepository.save(info);
    }

    private Info infoBuilder(HttpServletRequest request, long id) {
        String name = request.getParameter("name");
        String phonenumber = request.getParameter("phone_number");
        return new Info(id, name, phonenumber);
    }


    public List<Info> getUnPostList(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        List<Integer> postList = dailyPostRepository.getTodayPostIds(formatter.format(date));
        List<Integer> userList = userRepository.getAllUserId();
        userList.removeAll(postList);
        return infoRepository.getInfoByIds(userList);
    }

    public List<Info> getPostList(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        List<Integer> postIds = dailyPostRepository.getTodayPostIds(formatter.format(date));
        return infoRepository.getInfoByIds(postIds);
    }

    public DetailInfo getDetailInfoById(long id){
        return detailInfoRepository.getDetailInfoById(id);
    }

    public long getTodayPostNum(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        return dailyPostRepository.getTodayPostNum(formatter.format(date));
    }


    private DailyPost dailyPostBuilder(HttpServletRequest  request, long userid){
        String date = request.getParameter("date");
        DailyPost dailyPost= dailyPostRepository.getTodayPostWithId(date,userid);
        System.out.println(dailyPost);
        if (dailyPost != null){
            return null;
        }
        String address = request.getParameter("address");
        int health = Integer.parseInt(request.getParameter("health"));
        double temperature = Double.parseDouble(request.getParameter("temperature"));
        int been_to_danger_area = Integer.parseInt(request.getParameter("been_to_danger_area"));
        int contact_with_patient = Integer.parseInt(request.getParameter("contact_with_patient"));
        int vital_people = Integer.parseInt(request.getParameter("vital_people"));
        String remark = request.getParameter("remark");

        return new DailyPost(0, userid, temperature, health, been_to_danger_area, contact_with_patient, vital_people, remark, address, date);
    }
    private DetailInfo detailInfoBuilder(HttpServletRequest  request, long infoId){
        int licenceType = Integer.parseInt(request.getParameter("licence_type"));
        String licenceNumber = request.getParameter("licence_number");
        String emergencyContact = request.getParameter("emergency_contact");
        String emergencyCallNumber = request.getParameter("emergency_call_number");
        return new DetailInfo(infoId, licenceType, licenceNumber, emergencyContact, emergencyCallNumber);
    }


    public long getUserNum() {
        return userRepository.getUserNum();
    }
}
